package com.njit.mailm.core.controller;


import com.njit.mailm.commmon.constant.MailConstant;
import com.njit.mailm.commmon.utils.GUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.njit.mailm.commmon.utils.MD5Util;
import com.njit.mailm.commmon.utils.MailUtil;
import com.njit.mailm.core.bean.MessageWrap;
import com.njit.mailm.core.bean.User;
import com.njit.mailm.core.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * 用户①登录②注册Controller
 * @author Administrator
 *
 */
@RequestMapping("/")
@Controller
public class LoginController {

	@Autowired
	private UserService userService;
	/**
	 * 显示login页面
	 * @return login.jsp
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String startLogin(Model model, HttpSession session){
		model.addAttribute("warning", "none");
		Enumeration<String> e = session.getAttributeNames();
		while (e.hasMoreElements()) {
			String s = e.nextElement();
			if(s.equals("loginedUser")){
				model.addAttribute("loginedUser",session.getAttribute(s));

				return "redirect:/mail/index.shtml";
			}
		}

		return "login";
	}
	
	/**
	 * 注册界面显示
	 * @return register.jsp
	 */
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String startSignin(User user,Model model){
		model.addAttribute("warning", "none");

		user.setId(GUID.getGUID());
		if(user == null){
			model.addAttribute("user", new User());
		}else{
			model.addAttribute("user", user);
		}
		
		return "register";
	}
	
	/**
	 * 处理用户登录，比价用户名和密码是否对应
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String username,String password,boolean remember,Model model,HttpSession session){
		if(username.equals("")){
			model.addAttribute("warning", "block");
			model.addAttribute("error", "请输入用户名");
			return "login";
		}else if(password.equals("")){
			model.addAttribute("warning", "block");
			model.addAttribute("error", "请输入密码");
			return "login";
		}
		
		String encodedPwd = MD5Util.encode(password);
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(encodedPwd);
		User result = userService.comparePwd(user);
		if(result == null){
			model.addAttribute("warning", "block");
			model.addAttribute("error", "密码不正确");
			return "login";
		}else{
			model.addAttribute("user",result);

			session.setAttribute("loginedUser",result);
		}
		return "redirect:/mail/index.shtml";
	}
	/**
	 * 用户注册响应：
	 * 1、比较密码和重复密码
	 * 2、md5
	 * 3、存贮
	 * 4、处理错误
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String signin(User user,Model model){
		model.addAttribute("user", user);
		if(user.getUsername().equals("")||user.getPassword().equals("")||user.getRepeat().equals("")||user.getEmail().equals("")){
			model.addAttribute("err", "输入的信息不全");
			model.addAttribute("warning", "block");
			return "register";
		}
		//比较密码和重复密码
		if(!user.getPassword().equals(user.getRepeat())){
			model.addAttribute("err", "两次密码不一致");
			model.addAttribute("warning", "block");
			return "register";
		}
		
		
		String encodePwd = MD5Util.encode(user.getPassword());
		user.setPassword(encodePwd);
		try {
			userService.saveUser(user);
		} catch (Exception e) {
			e.getMessage();
			model.addAttribute("warning", "block");
			model.addAttribute("err", e.getMessage());
			return "register";
		}
		return "welcome";
	}
	
	/**
	 * 发送验证邮件到用户邮箱
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/auth",method=RequestMethod.GET)
	public String startAuth(String username){
		//TODO 发送邮件
		User user = userService.getUserByUsername(username);
		MessageWrap message = new MessageWrap();
		String body = "<a href='http://localhost:8080/mailm/auth.shtml?username="+username+"'>验证邮箱</a>";
		message.setTo(user.getEmail());
		message.setSubject("用户验证");
		message.setBody(body);
		MailUtil.htmlEmail(message);
		return "success";
	}

	/**
	 * 用户邮箱验证
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/auth",method=RequestMethod.POST)
	public String auth(String username){
		try {
			userService.setAuth(username);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	@RequestMapping(value = "logout",method = RequestMethod.GET)
	public String logout(HttpSession session){
		session.removeAttribute(MailConstant.CURRENTUSER);
		return "redirect:login.shtml";
	}

	
}
